<div class="px-2 m-2">
    <i class="fa fa-bullhorn mr-2"></i>发布新任务
    <hr class="mt-1 mx-0 mb-2">
    
    <form id="form-create-task">
        <div class="form-row">
            <div class="form-group col-md-4 col-lg-3">
                <label for="name">标题<b class="text-danger ml-2">*</b></label>
                <input type="text" class="form-control" id="name" name="name" placeholder="最大32个字符" required>
            </div>                    
                    
            <div class="form-group col-md-4 col-lg-3">
                <label for="pid">选择项目<b class="text-danger ml-2">*</b></label>
                <select id="pid" name="pid" class="form-control">
                    {% for _, info in ipairs(projs) do %}
                    <option value="{{info.id}}">{{info.name}}</option>
                    {% end %}
                </select>
            </div>

            <div class="form-group col-md-3 col-lg-2">
                <label for="weight">优先级<b class="text-danger ml-2">*</b></label>
                <select id="weight" name="weight" class="form-control">
                    {% for idx, info in ipairs(weights or {}) do %}
                    <option value="{{idx}}" class="{{info.color}}">{{info.title}}</option>
                    {% end %}
                </select>
            </div>
        </div>

        <div class="form-row">
            <div class="form-group col-md-3">
                <label for="assigned">指派成员<b class="text-danger ml-2">*</b></label>
                <select id="assigned" name="assigned" class="form-control">
                </select>
            </div>

            <div class="form-group col-md-3">
                <label for="cooperator">协作者/测试人员<b class="text-danger ml-2">*</b></label>
                <select id="cooperator" name="cooperator" class="form-control">
                </select>
            </div>
        </div>

        <div class="form-row">
            <div class="form-group col-md-3">
                <label for="start-time">计划开始时间<b class="text-danger ml-2">*</b></label>
                <input type="text" class="form-control form-datetime" id="start-time" name="start_time" required>
            </div>

            <div class="form-group col-md-3">
                <label for="end-time">计划结束时间<b class="text-danger ml-2">*</b></label>
                <input type="text" class="form-control form-datetime" id="end-time" name="end_time" required>
            </div> 
        </div>

        <div class="form-group">
            <label>任务标签</label><br>
            {% for idx, info in ipairs(tags or {}) do %}
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="checkbox" name="tags[]" value="{{idx}}">
                <span class="badge {{info.color}} mr-1">{{info.title}}</span><span class="text-muted">{{info.desc}}</span>
            </div>
            {% end %}
        </div>

        <div class="form-group">
            <label for="content">任务描述</label>
            <textarea id="content" name="content" data-provide="markdown" rows="12" data-savable="false" data-iconlibrary="fa" data-upload="/dashboard/files/upload"></textarea>
        </div>

        <div class="form-group">
            <div id="err"></div>
        </div>

        <div class="mt-4">
            <button id="btn-create-commit" type="button" class="btn btn-primary">确认发布</button>
            <button type="button" class="btn btn-secondary" onclick="return quit_create();">取  消</button>
        </div>
    </form>
</div>

<script>
    
$('#form-create-task .form-datetime').datetimepicker({
    format: 'Y-m-d',
    timepicker: false,
    lang: 'zh'
});

$('#form-create-task #content').markdown();

function get_members_of_proj(pid) {
    $('#form-create-task #assigned').empty();
    $.post('/dashboard/tasks/members_of_proj', {pid: pid}, function(ret) {
        $.each(ret, function(_, info) {
            $('#form-create-task #assigned').append('<option value="' + info.uid + '">【' + info.user_role + '】' + info.user + '</option>');
            $('#form-create-task #cooperator').append('<option value="' + info.uid + '">【' + info.user_role + '】' + info.user + '</option>');
        });
    }, 'json');
}

function quit_create() {
    last_filter_menu.click();
    $('.xdsoft_datetimepicker').remove();
}

$('#form-create-task #pid').change(function() {
    get_members_of_proj($('#form-create-task #pid').val());
})

$(document).ready(function() {
    get_members_of_proj($('#form-create-task #pid').val());
})

$('#btn-create-commit').click(function() {
    $.ajax({
        url: '/dashboard/tasks/do_create',
        type: 'POST',
        data: new FormData(document.getElementById('form-create-task')),
        dataType: 'json',
        processData: false,
        contentType: false,
        cache: false,
        success: function(rsp) {
            if (!rsp.ok) {
                showError('#form-create-task #err', '发布任务失败', rsp.err_msg);
            } else {
                $('#form-create-task #err').empty();
                location.href = '/dashboard/tasks/';
            }
        }
    });
});
</script>